package LeetCode.month02.day34;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class No274 {
    public static void main(String[] args) {
        System.out.println(hIndex(new int[]{1,1,3}));
    }
    //数组中有h个不小于h的值，求最大的h
    public static  int hIndex(int[] citations) {
        int n = citations.length;
        int[] count = new int[n+1];
        int res = 0;
        for (int i = 0; i < n; i++) {
            count[Math.min(citations[i],n)]++;
        }
        for (int i = count.length - 1; i >= 0; i--) {
            res+=count[i];
            if(res>=i) return i;
        }
        return 0;
//        int n = citations.length;
//        Arrays.sort(citations);
//        int res = 0;
//        System.out.println(Arrays.toString(citations));
//        for (int i = n - 1; i >= 0; i--) {
//            int x = citations[i];
//            if(x>res) res++;
//            else break;
//        }
//        return res;
    }
}
